﻿if ($.Article == null)
    $.Article = {};

$.Article.FileOpen = function() {
    $.manager.fileopen(1, "$.Article.FileOpen_Callback");
}

$.Article.FileOpen_Callback = function(fileurl) {
    $("input.articleFileUrl").val(fileurl);
}

$.Article.boardSN = 0;
$.Article.boardJsonData = "";
$.Article.isSetAdditionalData = false;

$.Article.getList = function() {
    $.ajaxNet(
		"/manager/board/boardmethod.aspx?_vb=GetListArticle",
		{
		    BoardSN : $.Article.boardSN,
		    PageNo: $("div.pager").pager("option", "currentPageNo"),
		    PageSize: $("div.pager").pager("option", "pageSize"),
		    Title_Search : ""
		}, function(results, context) {
    		$("table.articleList tbody").html(results[0]);
		    $("div.pager").pager("update", results[1]);
		});
};

$.Article.showInputForm = function(ArticleEntity) {
	if (!$.Article.isSetAdditionalData)
	{
		if ($.Article.boardJsonData.trim() != "")
		{
			var additionalDataNames = JSON.parse($.Article.boardJsonData);
			$.each(additionalDataNames.reverse(), function (idx, name) {
				$("div.inputForm table.inputTable").prepend("<tr style=\"height:30px;\">\
							<th style=\"width:100px; text-align:center\">"+ name +"</th>\
							<td><input type=\"text\" class=\"addtionalData\" style=\"width:300px\"/></td>\
						</tr>");
			});

			$.Article.isSetAdditionalData = true;
		}
	}
	
    if (typeof (ArticleEntity) == "undefined") {
        $("div.inputForm")
			.find("input.articleSN").val("").end()
			.find("input.articleTitle").val("").end()
			.find("textarea.articleContent").val("").end()
			.find("input.addtionalData").val("").end()
			.find("input.articleFileUrl").val("").end()
			.show();
    }
    else {
		if (ArticleEntity.strJsonData.trim() != "")
		{
			var additionalData = JSON.parse(ArticleEntity.strJsonData);
			
			$("div.inputForm input.addtionalData").each(function(idx, elem) {
				var elemName = $(elem).closest("tr").find("th").text();
				if (additionalData[elemName] != undefined && additionalData != "")
				{
					$(elem).val(additionalData[elemName]);
				}
			});
		}
        $("div.inputForm")
			.find("input.articleSN").val(ArticleEntity.n4ArticleSN).end()
			.find("input.articleTitle").val(ArticleEntity.strTitle).end()
			.find("textarea.articleContent").val(ArticleEntity.strContent).end()
			.find("input.articleFileUrl").val(ArticleEntity.strFileUrl).end()
			.show();
    }
}

$.Article.cancelInputForm = function() {
    $("div.inputForm").hide();
}

$.Article.submitInputForm = function() {
    var n1ArticleSN = $("div.inputForm input.articleSN").val();
    var strArticleTitle = $("div.inputForm input.articleTitle").val();
    var strArticleContent = $("div.inputForm textarea.articleContent").val();
    var strFileUrl = $("div.inputForm input.articleFileUrl").val();

	var addtionalData = new Object();
	
	$("div.inputForm input.addtionalData").each(function(idx, elem) {
		var elemName = $(elem).closest("tr").find("th").text();
		
		addtionalData[elemName] = $(elem).val();
	});
	
	var strArticleJsonData = JSON.stringify(addtionalData);
	
    if (n1ArticleSN != "") {
        $("div.inputForm").ajaxNet(
			"/manager/board/boardmethod.aspx?_vb=ModifyArticle",
			{
			    "ArticleSN": n1ArticleSN,
			    "Title": strArticleTitle,
			    "Content": strArticleContent,
			    "JsonData" : strArticleJsonData,
			    "FileUrl": strFileUrl
			}, function(results, context) {
			    $("div.inputForm").hide();
			    alert('수정되었습니다.');
			    $.Article.getList();
			});
    }
    else {
        $("div.inputForm").ajaxNet(
			"/manager/board/boardmethod.aspx?_vb=CreateArticle",
			{
			    "BoardSN" : $.Article.boardSN,
			    "Title": strArticleTitle,
			    "Content": strArticleContent,
			    "JsonData" : strArticleJsonData,
			    "FileUrl": strFileUrl
			}, function(results, context) {
			    $("div.inputForm").hide();
			    alert('추가되었습니다.');
			    $("div.pager").pager("pageNo", 1);
			    $.Article.getList();
			});
    }
}

$.Article.modify = function(elem) {
    var ArticleEntity = $(elem).closest("tr").tagdata();

    $("table.articleList").ajaxNet(
        "/manager/board/boardmethod.aspx?_vb=GetInfoArticle",
        {
            "ArticleSN": ArticleEntity.n4ArticleSN
        }, function(results, context) {
            $.Article.showInputForm(JSON.parse(results));
        });
};

$.Article.remove = function(elem) {
    var ArticleEntity = $(elem).closest("tr").tagdata();

    $("table.articleList").ajaxNet(
		"/manager/board/boardmethod.aspx?_vb=RemoveArticle",
		{
		    "ArticleSN": ArticleEntity.n4ArticleSN
		}, function(results, context) {
		    alert('삭제되었습니다.');
		    $.Article.getList();
		});
}